﻿using DG.Tweening;
using NL;
using NL.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace XGame.UI
{

    public class WinEquip : Window
    {
        

        public Button Button_Equip, Button_Remove,Button_Replace;

        public GameObject View1, View2;
        public float View12MoveInDistan = 1000;
        public float View12MoveTime = 0.4f;
        public Ease EaseType;
        public bool ShowDubugMessageBoxFor=true;
        Vector3 _View1OPos, _View2OPos;
        public void ShowView1()
        {
            if (View1 != null) 
            {
                View1.gameObject.SetActive(true);
                View1.transform.DOLocalMove(_View1OPos, View12MoveTime).SetEase(EaseType);
            }

            if(View2!=null )
                View2.transform.DOLocalMove(_View1OPos+new Vector3(View12MoveInDistan, 0,0), View12MoveTime).SetEase(EaseType);
        }
        public void ShowView2()
        {
            if (View1 != null) 
                View1.transform.DOLocalMove(_View1OPos - new Vector3(View12MoveInDistan, 0, 0), View12MoveTime).SetEase(EaseType);
            if (View2 != null)
            {
                View2.gameObject.SetActive(true);
                View2.transform.DOLocalMove(_View1OPos, View12MoveTime).SetEase(EaseType);
            }
               
        }

        protected override void Start()
        {
            base.Start();



            if (Button_Equip != null)
            {
                Button_Equip?.onClick.AddListener(() =>
                {
                    _DoEquipOn();
                });
            }
            if (Button_Replace != null)
            {
                Button_Replace?.onClick.AddListener(() =>
                {
                    _DoEquipOn();
                });
            }
            if (Button_Remove != null)
            {
                Button_Remove?.onClick.AddListener(() =>
                {
                    _DoEquipOff();
                });
            }

        }
        void _DoEquipOn()
        {
            if (_ONEquipOn != null && _Owner!=null)
            {
                string _res = _ONEquipOn.Invoke(_Owner);
                if (string.IsNullOrEmpty(_res))
                {
                    if(ShowDubugMessageBoxFor)
                        NL.UI.UI.ShowBox("Success", $"换装完成 @[{_Owner.name}]");
                    //装上装备
                   
                }
                else
                {
                    if (ShowDubugMessageBoxFor)
                        NL.UI.UI.ShowBox("Failed", $"换装失败:[{_res}]@ [{_Owner.name}]").SetTitleColor(Color.red);
                }
                LOG.Add($"[WinEquip._DoEquipOn]:{_res}");
            }

            Close();
        }
        void _DoEquipOff()
        {
            if (_ONEquipOff != null && _Owner != null)
            {
                string _res = _ONEquipOff.Invoke(_Owner);

                if (string.IsNullOrEmpty(_res))
                {
                    if (ShowDubugMessageBoxFor)
                        //NL.UI.UI.ShowBox("Success", $"卸载装备完成 @[{_Owner.name}]");
                        NL.UI.UI.ShowTiper( $"卸载装备完成 @[{_Owner.name}]");
                }
                else
                {
                    if (ShowDubugMessageBoxFor)
                        //NL.UI.UI.ShowBox("Failed", $"卸载装备失败:[{_res}]@ [{_Owner.name}]").SetTitleColor(Color.red);
                        NL.UI.UI.ShowTiper($"卸载装备失败:[{_res}]@ [{_Owner.name}]");
                }
                LOG.Add($"[WinEquip._DoEquipOff]:{_res}");
            }
            Close();
        }

        protected override void OnShow()
        {
            base.OnShow();

            // 检测 该物品是都有 已经装备. 的.

            if (Button_Equip != null) Button_Equip.gameObject.SetActive(_Target == null && _Target != _Owner);
            if (Button_Replace != null) Button_Replace.gameObject.SetActive(_Target != null && _Target != _Owner);
            if (Button_Remove != null) Button_Remove.gameObject.SetActive(_Target==_Owner);

            if (View1 != null)
            {
                View1.SetActive(true);
                _View1OPos = View1.transform.localPosition;
            }
            if (View2 != null)
            {
                View2.SetActive(false);
                _View2OPos = View2.transform.localPosition;
                View2.transform.localPosition = _View2OPos + new Vector3(View12MoveInDistan, 0, 0);
            }

            Set(_Owner == null ? new NLIni() : _Owner.INFO());



        }
        // 已经装备的 实例ID
        GameObject _Owner, _Target;
        public WinEquip SetTarget(GameObject _ob)
        {
            _Target = _ob; return this;
        }

        public WinEquip SetOwner(GameObject _ob)
        {
            _Owner = _ob; return this;
        }



        EquipAction< GameObject> _ONEquipOn, _ONEquipOff;
        public WinEquip WaitEquipOn(EquipAction<GameObject> _on)
        {
            _ONEquipOn = _on; return this;
        }
        public WinEquip WaitEquipOff(EquipAction<GameObject> _on)
        {
            _ONEquipOff = _on; return this;
        }


    }
}

